var mongogdb = require('./db');
var Comment = require('./comment');
var markdown = require('markdown').markdown;
var marked = require('marked');
var getTime = require('./date');
marked.setOptions({
  renderer: new marked.Renderer(),
  gfm: true,
  tables: true,
  breaks: true,
  pedantic: false,
  sanitize: true,
  smartLists: true,
  smartypants: false
});
function Post(post){
  this.title = post.title;
  this.time = null;
  this.author = 'ZSK';
  this.avatar = 'images/avatar.jpg';
  this.showImg = post.showImg;
  this.simIntro = post.simIntro;
  this.loveNum = post.loveNum;
  this.commentNum = post.commentNum;
  this.content = post.content;
  this.url = post.url;
  this.kind = post.kind;
  this.articleId = post.articleId;
  this.comment = post.comment;
}

Post.prototype = {
  save:function(callback){

    this.time = getTime();
    var post = {
      title:this.title,
      time:this.time.minute,
      author:this.author,
      content:this.content,
      avatar:this.avatar,
      showImg:this.showImg,
      simIntro:this.simIntro,
      loveNum:this.loveNum,
      commentNum:this.commentNum,
      url:this.url,
      kind:this.kind,
      articleId:this.articleId,
      comment:this.comment
    };
    mongogdb.open(function(err,db){
      if(err){
        return callback(err);
      }
      db.collection("post",function(err,collection){
        if(err){
          mongogdb.close();
          return callback(err);
        }
        collection.insert(post,{safe:true}, function (err) {
          //mongogdb.close();
          if(err){
            return callback(err);
          }
          callback(null);
        })
      })
    })
  }

};
Post.getAll = function (callback) {
  mongogdb.open(function (err, db) {
    if(err){
      return callback(err);
    }
    db.collection("post",function(err,collection){
      if(err){
        return callback(err);
      }
      collection.find().sort({time:-1}).toArray(function(err,posts){
        //mongogdb.close();
        if(err){
          return callback(err);
        }
        posts.forEach(function(post){
          post.content = markdown.toHTML(post.content);
        });
        callback(null,posts);
      });
    });
  });
};
Post.getAllToEdit = function (callback) {
  mongogdb.open(function (err, db) {
    if(err){
      return callback(err);
    }
    db.collection("post",function(err,collection){
      if(err){
        return callback(err);
      }
      collection.find().sort({time:-1}).toArray(function(err,posts){
        //mongogdb.close();
        if(err){
          return callback(err);
        }
        callback(null,posts);
      });
    });
  });
};
Post.getOne = function(articleId,callback){
  console.log(articleId);
  mongogdb.open(function (err, db) {
    if(err){
      return callback(err);
    }
    db.collection("post",function(err,collection){
      if(err){
        return callback(err);
      }
      collection.findOne({articleId:articleId},function(err,post){
        //mongogdb.close();
        if(err){
          return callback(err);
        }
        console.log(post);
        if(!post){
          callback(null,"noTheArticle",null);
        }else{
          post.content = marked(post.content);
          callback(null,"success",post);
        }

      });
    });
  });
};
Post.getPage = function (pageRequest, callback) {
  mongogdb.open(function (err, db) {
    if(err){
      return callback(err);
    }
    db.collection("post",function(err,collection){
      if(err){
        return callback(err);
      }
      collection.find().sort({time:-1}).skip(pageRequest.startNum).limit(pageRequest.itemCount).toArray(function(err,posts){
        //mongogdb.close();
        if(err){
          return callback(err);
        }

        posts.forEach(function(post){
          post.content = markdown.toHTML(post.content);
        });
        callback(null,posts);
      });
    });
  });
};
Post.update = function(articleId,updateObj,callback){
  mongogdb.open(function (err, db) {
    if(err){
      return callback(err);
    }
    db.collection("post",function(err,collection){
      if(err){
        return callback(err);
      }
      collection.update({articleId:articleId},{$set:updateObj},function(err){
        //mongogdb.close();
        if(err){
          return callback(err);
        }
        callback(null,'success');
      });
    });
  });
};
Post.delete = function(articleId,callback){
  mongogdb.open(function (err, db) {
    if(err){
      return callback(err);
    }
    db.collection("post",function(err,collection){
      if(err){
        return callback(err);
      }
      collection.remove({articleId:articleId},function(err){
        //mongogdb.close();
        if(err){
          console.log(err);
          return callback(null,'failure');
        }
        callback(null,'success');
      });

    });
  });
};
Post.search = function(query,callback){
  mongogdb.open(function(er,db){
    if(err){
      return callback(err);
    }
    db.collection('post',function(err,collection){
      if(err){
        return callback(err);
      }
      collection.find({title:{$regex:query}}).toArray(function(err,posts){
        if(err){
          callback(err,{success:false,posts:[]});
          return callback(err);
        }
        if(posts.length){
          callback(null,{success:true,posts:posts});
        }else{
          callback(null,{success:false,posts:[]});
        }

      });
    })
  })
};


module.exports = Post;
